﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using NcgmpDataAccess.Utilities;

namespace NcgmpDataAccess
{
    public class MapUnitPolysAccess : NcgmpAccess
    {
        private Dictionary<string, int> m_fieldIndices = new Dictionary<string, int>();

        private string m_tableName;

        public override string TableName
        {
            get { return m_tableName; }
        }
        private bool m_isFeatureClass;

        public override bool IsFeatureClass
        {
            get { return m_isFeatureClass; }
        }

        public MapUnitPolysAccess(IWorkspace theWorkspace)
        {
            TheWorkspace = theWorkspace;

            m_tableName = "MapUnitPolys";
            m_isFeatureClass = true;

            try
            {
                IFeatureClass theFeatureClass = commonFunctions.OpenFeatureClass(theWorkspace, "MapUnitPolys");
                m_fieldIndices.Add("MapUnitPolys_ID", theFeatureClass.FindField("MapUnitPolys_ID"));
                m_fieldIndices.Add("MapUnit", theFeatureClass.FindField("MapUnit"));
                m_fieldIndices.Add("Label", theFeatureClass.FindField("Label"));
                m_fieldIndices.Add("IdentityConfidence", theFeatureClass.FindField("IdentityConfidence"));
                m_fieldIndices.Add("Notes", theFeatureClass.FindField("Notes"));
                m_fieldIndices.Add("Symbol", theFeatureClass.FindField("Symbol"));
                m_fieldIndices.Add("DataSourceID", theFeatureClass.FindField("DataSourceID"));
            }
            catch (Exception err) { throw err; }
        }

        public override NcgmpFeature CreateFeature(Dictionary<string, object> attributeNameValuePairs)
        {
            MapUnitPolysFeature thePolyFeature = new MapUnitPolysFeature();
            thePolyFeature.Identifier = attributeNameValuePairs["MapUnitPolys_ID"].ToString();
            thePolyFeature.MapUnit = attributeNameValuePairs["MapUnit"].ToString();
            thePolyFeature.Label = attributeNameValuePairs["Label"].ToString();
            thePolyFeature.IdentityConfidence = attributeNameValuePairs["IdentityConfidence"].ToString();
            thePolyFeature.Notes = attributeNameValuePairs["Notes"].ToString();
            thePolyFeature.Symbol = attributeNameValuePairs["Symbol"].ToString();
            thePolyFeature.DataSourceID = attributeNameValuePairs["DataSourceID"].ToString();
            thePolyFeature.Shape = attributeNameValuePairs["Shape"] as IPolygon;

            IFeatureClass theFeatureClass = commonFunctions.OpenFeatureClass(TheWorkspace, TableName);
            IFeatureCursor theInsertCursor = theFeatureClass.Insert(true);
            IFeatureBuffer theFeatureBuffer = theFeatureClass.CreateFeatureBuffer();

            theFeatureBuffer.set_Value(m_fieldIndices["MapUnitPolys_ID"], thePolyFeature.Identifier);
            theFeatureBuffer.set_Value(m_fieldIndices["MapUnit"], thePolyFeature.MapUnit);
            theFeatureBuffer.set_Value(m_fieldIndices["Label"], thePolyFeature.Label);
            theFeatureBuffer.set_Value(m_fieldIndices["IdentityConfidence"], thePolyFeature.IdentityConfidence);
            theFeatureBuffer.set_Value(m_fieldIndices["Notes"], thePolyFeature.Notes);
            theFeatureBuffer.set_Value(m_fieldIndices["Symbol"], thePolyFeature.Symbol);
            theFeatureBuffer.set_Value(m_fieldIndices["DataSourceID"], thePolyFeature.DataSourceID);
            theFeatureBuffer.Shape = thePolyFeature.Shape as IGeometry;

            theInsertCursor.InsertFeature(theFeatureBuffer);
            return thePolyFeature;
        }

        public override bool SaveFeatures()
        {
            throw new System.NotImplementedException();
        }

        public override bool UpdateFeatures(NcgmpFeature theFeature)
        {
            throw new System.NotImplementedException();
        }

        public override NcgmpFeature ConstructTypedFeature(IFeature ArcObjectsFeature)
        {
            try
            {
                MapUnitPolysFeature thePolyFeature = new MapUnitPolysFeature();
                thePolyFeature.Identifier = ArcObjectsFeature.get_Value(m_fieldIndices["MapUnitPolys_ID"]).ToString();
                thePolyFeature.MapUnit = ArcObjectsFeature.get_Value(m_fieldIndices["MapUnit"]).ToString();
                thePolyFeature.Label = ArcObjectsFeature.get_Value(m_fieldIndices["Label"]).ToString();
                thePolyFeature.IdentityConfidence = ArcObjectsFeature.get_Value(m_fieldIndices["IdentityConfidence"]).ToString();
                thePolyFeature.Notes = ArcObjectsFeature.get_Value(m_fieldIndices["Notes"]).ToString();
                thePolyFeature.Symbol = ArcObjectsFeature.get_Value(m_fieldIndices["Symbol"]).ToString();
                thePolyFeature.DataSourceID = ArcObjectsFeature.get_Value(m_fieldIndices["DataSourceID"]).ToString();
                thePolyFeature.Shape = ArcObjectsFeature.Shape as IPolygon;

                return thePolyFeature as NcgmpFeature;
            }
            catch (Exception err) { throw err; }
        }

        public override NcgmpFeature ConstructTypedRow(IRow ArcObjectsRow)
        {
            throw new System.NotImplementedException();
        }
    }
}
